<?php

namespace app\index\controller;

use app\index\model\DetailModel;
use think\Controller;
use app\index\controller\Basecontroller;

class Getdetail extends Basecontroller
{
  private $Model;

  public function __construct(){
      $this ->Model = new DetailModel();
      parent::__construct();
  }
  
  public function select(){//获取商品详情
    $gid = getPost()['id'];
    $res = $this->Model->select($gid);
    if($res){
      //进度
      $pace = (float)number_format($res['g_start_money']/$res['g_end_money']*100,1);//计算进度
      $res['g_pace'] = $pace;//给对象增加进度属性
      //到期日期
      $endTime = (float)$res['start_time']+(float)$res['keep_time'];
      $endDate = date('Y-m-d',$endTime);
      $res['g_end_date'] = $endDate;
      //剩余天数
      $nowDate = strtotime(date('Y-m-d',time()));
      $endDay = strtotime($endDate);
      $int = $endDay - $nowDate;
      if($int<=0){
        $res['g_remaining_day'] = 0;
      }else{
        $days = round(($endDay - $nowDate)/3600/24);
        $res['g_remaining_day'] = $days;
      }
      $token = getheaders()['Access-Token'];
      $userRes = $this->redis->get($token);
      $userRes = json_decode($userRes, true);
      $isfollowers = $this->Model->change($gid,$userRes['uid'],2000);
      if($isfollowers){
        $res['isfollowers'] = '已关注';
      }else{
        $res['isfollowers'] = '关注';
      }
      $islikes = $this->Model->change($gid,$userRes['uid'],2001);
      if($islikes){
        $res['islikes'] = '已点赞';
      }else{
        $res['islikes'] = '点赞';
      }
      return res(1000,"获取成功",$res);
    }else{
      return res(1001,"获取失败");
    }
  }
  public function change(){//点赞关注
    $change_id = getPost()['change_id'];
    $gid = getPost()['gid'];
    $token = getheaders()['Access-Token'];
    if (!$token) {
      return res('1001', 'token 无效');
    }
    $res = $this->redis->get($token);
    if (!$res) {
      return res('1001', 'token 无效');
    }
    $res = json_decode($res, true);
    $changeRes = $this->Model->change($gid,$res['uid'],$change_id);
    if($changeRes){
      if($change_id==2001){
        return res(1001,'已点赞');
      }else if($change_id==2000){
        return res(1001,'已关注');
      }
    }else{
      $addChange = $this->Model->addChange($gid,$res['uid'],$change_id);
      if($addChange){
        if($change_id==2001){//点赞
          $goodsLikes = $this->Model->goodsLikes($gid,'likes_num');//获取原来数量
          $newNum = $goodsLikes+1;
          $goodsRes = $this->Model->upNum($gid,'likes_num',$newNum);
          if($goodsRes){
            return res(1000,'点赞成功');
          }else{
            return res(1001,'点赞失败');
          }
        }else if($change_id==2000){//关注
          $goodsLikes = $this->Model->goodsLikes($gid,'followers_num');//获取原来数量
          $newNum = $goodsLikes+1;
          $goodsRes = $this->Model->upNum($gid,'followers_num',$newNum);
          if($goodsRes){
            return res(1000,'关注成功');
          }else{
            return res(1001,'关注失败');
          }
        }
      }else{
        if($change_id==2001){
          return res(1001,'点赞失败');
        }else if($change_id==2000){
          return res(1001,'关注失败');
        }
      }
    }
  }
}